package noodws

import "sync"

var wsHandlerMapMutex sync.Mutex //路由锁
var whiteListMutex sync.Mutex    //白名单锁
var wsHandlerMap = map[string]WsHandler{}
var whiteList = map[string]bool{}

// 添加白名单
func setWhiteList(cmd string) {
	whiteListMutex.Lock()
	defer whiteListMutex.Unlock()
	whiteList[cmd] = true
}

// 只有初始化项目时才有写操作，所以读不加锁
func getWhiteList(cmd string) (bool, bool) {
	value, ok := whiteList[cmd]
	return value, ok
}

// 添加路由
func setWsHandlerMap(cmd string, fun WsHandler) {
	wsHandlerMapMutex.Lock()
	defer wsHandlerMapMutex.Unlock()
	wsHandlerMap[cmd] = fun
}

// 只有初始化项目时才有写操作，所以读不加锁
func getWsHandlerMap(cmd string) (WsHandler, bool) {
	value, ok := wsHandlerMap[cmd]
	return value, ok
}

// 注册为白名单路由
func RegisterWhiteRouter(cmd string, fun WsHandler) {
	setWsHandlerMap(cmd, fun)
	setWhiteList(cmd)
}

// 注册为普通路由
func RegisterRouter(cmd string, fun WsHandler) {
	setWsHandlerMap(cmd, fun)
}
